package com.fmatos.amazingwallpapers.test.helper;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.fmatos.amazingwallpapers.image.network.CloudClient;
import com.fmatos.amazingwallpapers.image.network.CloudHelper;
import com.fmatos.amazingwallpapers.image.network.dto.RemoteAlbumInfo;
import com.fmatos.amazingwallpapers.image.network.dto.RemotePhoto;
import com.google.gson.Gson;

public class NetworkMocks {
	
	// basic tests
	public static final String URL_NOT_MOCKED = "http://unmockedUrl.com";
	
	public static final String URL_TEST = "http://google.com";
	public static final String DATA_TEST = "Hello mocked world";

	// albums
	public static final String URL_ALL_ALBUMS = "/all_albums_list";
	
	public static final String ALBUM_NAME = "whatever_album";
	public static final String URL_ALBUM = "/an_album/whatever_url";
	public static final String PHOTO_CAPTION = "whatever caption";
	
	

	public static CloudHelper buildMockedCloudHelper() {

		NetworkMocks networkMocks = new NetworkMocks();
		CloudHelper cloudHelper = mock(CloudHelper.class);

		// very basic mocks
		URLConnection simpleTest = networkMocks.buildConnectionForObject(DATA_TEST);
		when(cloudHelper.connectToUrl(URL_TEST)).thenReturn(simpleTest);
		when(cloudHelper.connectToUrl(URL_NOT_MOCKED)).thenReturn(null);
		
		// album mocking
		URLConnection connectionForAlbum = networkMocks.buildConnectionForAlbum();
		when(cloudHelper.connectToUrl(CloudClient.HEROKU_BASE_URL + URL_ALBUM)).thenReturn(connectionForAlbum );
		URLConnection connectionForAlbumList = networkMocks.buildConnectionForAlbumList();
		when(cloudHelper.connectToUrl(CloudClient.HEROKU_BASE_URL + URL_ALL_ALBUMS)).thenReturn(connectionForAlbumList  );


		return cloudHelper;
	}
	

	private URLConnection buildConnectionForAlbumList() {
		
		List<RemoteAlbumInfo> albumList = new LinkedList<RemoteAlbumInfo>();
		albumList.add(buildAlbum());
		
		return buildConnectionForObject(albumList);

	}

	
	private URLConnection buildConnectionForAlbum() {
	
		return buildConnectionForObject(buildAlbum());
	}
	
	public URLConnection buildConnectionForObject(Object object) {
		URLConnection connection = mock(URLConnection.class);

		Gson gson = new Gson();
		
		
		InputStream stubInputStream = 
			     IOUtils.toInputStream(gson.toJson(object).toString());
		
		try {
			when(connection.getInputStream()).thenReturn(stubInputStream );
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		return connection;
	}
	

	private RemoteAlbumInfo buildAlbum() {
		RemoteAlbumInfo remoteAlbum = new RemoteAlbumInfo();

		remoteAlbum.setName(ALBUM_NAME);
		remoteAlbum.setPhotos(new LinkedList<RemotePhoto>());
		RemotePhoto photo = new RemotePhoto();
		photo.setCaption(PHOTO_CAPTION);
		remoteAlbum.getPhotos().add(photo );


		return remoteAlbum;
	}
}
